package com.briup.srms.base.web.controller;

import com.briup.srms.base.bean.BasePrivilege;
import com.briup.srms.base.bean.BaseUser;
import com.briup.srms.base.service.IBasePrivilegeService;
import com.briup.srms.base.service.IBaseUserService;
import com.briup.srms.base.vm.UserRoleVM;
import com.jacky.base.utils.Message;
import com.jacky.base.utils.MessageUtil;
import com.jacky.base.utils.PageVM;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: kindergarten
 * @description: 用户基础控制器类
 * @author: jacky
 * @create: 2020-11-16 16:23
 **/
@Api(description="用户相关接口")
@Validated
@RestController
@RequestMapping("/baseUser")
public class BaseUserController {
    @Autowired
    private IBaseUserService baseUserService;
    @Autowired
    private IBasePrivilegeService basePrivilegeService;

    @ApiOperation(value = "根据用户名角色名分页查询用户信息")
    @GetMapping("pageQuery")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"),
        @ApiImplicitParam(name = "username", value = "用户名", paramType = "query"),
        @ApiImplicitParam(name = "rolename", value = "角色名", paramType = "query")
	})
    public Message pageQuery(int page, int pageSize, String username,String rolename){
       PageVM<BaseUser> pageVM = baseUserService.pageQuery(page, pageSize, username, rolename);
       return MessageUtil.success(pageVM);
    }

    @ApiOperation(value = "通过ID查询权限信息")
    @GetMapping("findMenuByUserId")
    public Message findMenuByUserId(@RequestParam(name = "id") long id){
        List<BasePrivilege> list =  basePrivilegeService.findMenuByUserId(id);
        return MessageUtil.success(list);
    }

    @ApiOperation(value = "保存或更新")
    @PostMapping(value = "saveOrUpdate")
    public Message saveOrUpdate(BaseUser baseUser){
        baseUserService.saveOrUpdate(baseUser);
        return MessageUtil.success("更新成功");
    }

    @ApiOperation(value = "通过id删除")
    @GetMapping(value = "deleteById")
    public Message deleteById(long id){
        baseUserService.deleteById(id);
        return MessageUtil.success("删除成功");
    }

    @ApiOperation(value = "设置权限")
    @PostMapping(value = "setRoles")
    public Message setRoles(UserRoleVM userRoleVM){
        System.out.println(userRoleVM);
        baseUserService.setRoles(userRoleVM.getId(),userRoleVM.getRoles());
        return MessageUtil.success("设置成功");
    }

    @ApiOperation(value = "保存用户并指定角色")
    @PostMapping(value = "addUserWidthRole")
    public Message addUserWidthRole(BaseUser baseUser,long roleId){
        baseUserService.addUserWidthRole(baseUser, roleId);
        return MessageUtil.success("保存成功");
    }
    @ApiOperation(value = "修改用户头像")
    @PostMapping(value = "alterUserface")
    public Message alterUserface(long id,String userface){
        baseUserService.alterUserface(id,userface);
        return MessageUtil.success("保存成功");
    }
}
